//
//  StarRateView.m
//  ImGuider
//
//  Created by llt on 2017/4/18.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "StarRateView.h"





typedef void(^completeBlock)(CGFloat currentScore);

@interface StarRateView()

@property (nonatomic, strong) UIView *foregroundStarView;
@property (nonatomic, strong) UIView *backgroundStarView;

@property (nonatomic, assign) NSInteger numberOfStars;

@property (nonatomic, strong) completeBlock complete;

@property (nonatomic, assign) CGFloat currentScore;   // 当前评分：0-5  默认0

@property (nonatomic) BOOL isAnimation;

@end

@implementation StarRateView

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    _numberOfStars = kNUMBER_OF_STAR;
    [self createStarView];
}
#pragma mark - 代理方式
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = kNUMBER_OF_STAR;
        [self createStarView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame numberOfStars:(int)number{
    if (self = [super initWithFrame:frame]) {
        _numberOfStars = number;
        [self createStarView];
    }
    return self;
}

- (void)setScore:(float)score {
    
    NSInteger sc = ceilf(score);
    self.currentScore = sc;
}

- (void)setScore:(float)score withAnimation:(bool)isAnimate {
    
    self.isAnimation = isAnimate;
    self.currentScore = score;
}

#pragma mark - private Method
-(void)createStarView{
    
    self.foregroundStarView = [self createStarViewWithImage:kFOREGROUND_STAR];
    self.backgroundStarView = [self createStarViewWithImage:kBACKGROUND_STAR];
    self.foregroundStarView.frame = CGRectMake(0, 0, self.bounds.size.width*_currentScore/self.numberOfStars, self.bounds.size.height);
    
    [self addSubview:self.backgroundStarView];
    [self addSubview:self.foregroundStarView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapRateView:)];
    
    tapGesture.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGesture];
    
}

- (UIView *)createStarViewWithImage:(NSString *)imageName {
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    view.clipsToBounds = YES;
    view.backgroundColor = [UIColor clearColor];
    for (NSInteger i = 0; i < self.numberOfStars; i ++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * self.bounds.size.width / self.numberOfStars, 0, self.bounds.size.width / self.numberOfStars, self.bounds.size.height);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imageView];
    }
    return view;
}

- (void)userTapRateView:(UITapGestureRecognizer *)gesture {
    
    if (!self.canRating) {
        return;
    }
    CGPoint tapPoint = [gesture locationInView:self];
    CGFloat offset = tapPoint.x;
    CGFloat realStarScore = offset / (self.bounds.size.width / self.numberOfStars);
    self.isAnimation = YES;
    self.currentScore = ceilf(realStarScore);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    @weakify(self);
    CGFloat animationTimeInterval = self.isAnimation ? 0.2 : 0;
    [UIView animateWithDuration:animationTimeInterval animations:^{
        @strongify(self);
        
        self.foregroundStarView.frame = CGRectMake(0, 0, self.bounds.size.width * self.currentScore/self.numberOfStars, self.bounds.size.height);
    }];
}


-(void)setCurrentScore:(CGFloat)currentScore {
    
    if (_currentScore == currentScore) {
        return;
    }
    if (currentScore < 0) {
        _currentScore = 0;
    } else if (currentScore > _numberOfStars) {
        _currentScore = _numberOfStars;
    } else {
        _currentScore = currentScore;
    }
    
    if ([self.delegate respondsToSelector:@selector(starRatingView:score:)]) {
        [self.delegate starRatingView:self score:_currentScore];
    }
    [self setNeedsLayout];
}


@end
